﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Reader
{
    public partial class shujiMain : Form
    {
        public shujiMain()
        {
            InitializeComponent();
        }


        private void ReaderMain_Load(object sender, EventArgs e)
        {
            //一进来查询窗体的时候。给显示数据的控件绑定数据
            string sql = "select bookName,bookZuozhe,Fenlei,bookTime,z.bookZhuangtai from books b join Fenlei f on b.bookFenlei=f.FenleiID join BookZhuangtai z on b.bookZhuangtai=z.bookZhuangtaiID";
            DataTable dt = DBHelper.GetDataTable(sql);
            dgv_showinfo.DataSource = dt;

            SqlConnection con = new SqlConnection("server=.;database=booksDB;Uid = sa; Pwd = wang0118");
            SqlDataAdapter da = new SqlDataAdapter("select Fenlei from Fenlei", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "Fenlei");
            DataTable dtGroup = ds.Tables["Fenlei"];

            cobtype.DataSource = dtGroup;
            cobtype.DisplayMember = "Fenlei";
            cobtype.ValueMember = "Fenlei";
        }

        private void btselect_Click(object sender, EventArgs e)
        {
            if (cobtype.Text == "")
            {
                if (txtName.Text == "")
                {
                    MessageBox.Show("请输入关键字");
                    return;//直接返回窗体
                }
                string keyword = txtName.Text;//获取到用户输入的关键字
                string sql = string.Format("select bookName,bookZuozhe,Fenlei,bookTime,z.bookZhuangtai from books b join Fenlei f on b.bookFenlei=f.FenleiID join BookZhuangtai z on b.bookZhuangtai=z.bookZhuangtaiID where bookName like '%{0}%'", keyword);//如果不为空则直接查询
                DataTable dt = DBHelper.GetDataTable(sql);
                if (dt.Rows.Count == 0)
                {
                    dgv_showinfo.DataSource = dt;//清空数据
                    MessageBox.Show("没查询到关于" + keyword + "的书籍信息");

                }
                dgv_showinfo.DataSource = dt;//找到了数据就绑定数据
            }
            else
            {
                if (txtName.Text == "")
                {
                    string type = cobtype.Text;
                    string sql = string.Format("select bookName,bookZuozhe,Fenlei,bookTime,z.bookZhuangtai from books b join Fenlei f on b.bookFenlei=f.FenleiID join BookZhuangtai z on b.bookZhuangtai=z.bookZhuangtaiID where Fenlei='{0}'", type);
                    DataTable dt = DBHelper.GetDataTable(sql);
                    if (dt.Rows.Count == 0)
                    {
                        dgv_showinfo.DataSource = dt;//清空数据
                        MessageBox.Show("没查询到关于" + type + "的分类信息");
                    }
                    dgv_showinfo.DataSource = dt;//找到了数据就绑定数据
                }
                else
                {
                    string type = cobtype.Text;
                    string keyword = txtName.Text;
                    string sql = string.Format("select bookName,bookZuozhe,Fenlei,bookTime,z.bookZhuangtai from books b join Fenlei f on b.bookFenlei=f.FenleiID join BookZhuangtai z on b.bookZhuangtai=z.bookZhuangtaiID where bookName like '%{0}%' and Fenlei='{1}'", keyword, type);
                    DataTable dt = DBHelper.GetDataTable(sql);
                    if (dt.Rows.Count == 0)
                    {
                        dgv_showinfo.DataSource = dt;//清空数据
                        MessageBox.Show("没查询到关于" + type + "类别中" + keyword + "的书籍信息");
                    }
                    dgv_showinfo.DataSource = dt;//找到了数据就绑定数据 
                }
            }
        }
    }
}
